set more off
clear all
set matsize 2000
ssc install newey2
ssc install ivreg2
ssc install ranktest

*NW assumes an AR(1) error process, which because of fixed-effects and perpetual inventory method to generate all R&D stocks is what any error structure is likely to look
*Clustering is an alternative but allows for full cross-period error dependence, which in long-panels of yearly data with FEs and moderate sized N is over conservative
*To get first stage F-tests, noting that newey2 does not produce these so need to use ivreg2 with clustering (which is conservative)

log using rnd_regression_output, replace

*************************************
*Table 2: Model A
*************************************
clear all
insheet using ".\stata_data1\data1_0_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year*, cluster(pid) partial(year*)

*************************************
*Table 2: Model B
*************************************
clear all
insheet using ".\stata_data1\data1_0_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table 3: Model C
*************************************
clear all
insheet using ".\stata_data1\data1_0_5_1.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 z3 = q1 q2 q3) year*, force lag(1)
ivreg2 y (z1 z2 z3 = q1 q2 q3) year*, cluster(pid) partial(year*)

*************************************
*Table 3: Model D
*************************************
clear all
insheet using ".\stata_data1\data1_0_5_1.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 z3 = q1 q2 q3) year* firm*, force lag(1)
ivreg2 y (z1 z2 z3 = q1 q2 q3) year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table 4: Jaffe
*************************************
clear all
insheet using ".\stata_data1\linkdata_1.csv"

logit y z*

*************************************
*Table 4: Mahalanobis
*************************************
clear all
insheet using ".\stata_data1\linkdata_0.csv"

logit y z*

*************************************
*Table 5: Jaffe
*************************************
clear all
insheet using ".\stata_data1\data2_1.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table 5: Mahalanobis
*************************************
clear all
insheet using ".\stata_data1\data2_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table 6: Jaffe
*************************************
clear all
insheet using ".\stata_data2\data_2.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, force lag(1)
ivreg2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table 6: Mahalanobis
*************************************
clear all
insheet using ".\stata_data2\data_3.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, force lag(1)
ivreg2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.3: 3 years
*************************************
clear all
insheet using ".\stata_data1\data1_0_3_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.3: 4 years
*************************************
clear all
insheet using ".\stata_data1\data1_0_4_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.3: 5 years
*************************************
clear all
insheet using ".\stata_data1\data1_0_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.3: 6 years
*************************************
clear all
insheet using ".\stata_data1\data1_0_6_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.3: 7 years
*************************************
clear all
insheet using ".\stata_data1\data1_0_7_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.4: 3 years
*************************************
clear all
insheet using ".\stata_data1\data1_1_3_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.4: 4 years
*************************************
clear all
insheet using ".\stata_data1\data1_1_4_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.4: 5 years
*************************************
clear all
insheet using ".\stata_data1\data1_1_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.4: 6 years
*************************************
clear all
insheet using ".\stata_data1\data1_1_6_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.4: 7 years
*************************************
clear all
insheet using ".\stata_data1\data1_1_7_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.5: Manufacturing
*************************************
clear all
insheet using ".\stata_data1\data1_m_0_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.5: Services
*************************************
clear all
insheet using ".\stata_data1\data1_s_0_5_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.6
*************************************
clear all
insheet using ".\stata_data2\data_io_4.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, force lag(1)
ivreg2 y (z1 z2 z3 = q1 q2 q3) z4 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.7: Jaffe
*************************************
clear all
insheet using ".\stata_data1\linkdata_io_1.csv"

logit y z*

*************************************
*Table J.7: Mahalanobis
*************************************
clear all
insheet using ".\stata_data1\linkdata_io_0.csv"

logit y z*

*************************************
*Table J.8: Jaffe
*************************************
clear all
insheet using ".\stata_data1\data2_io_1.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.8: Mahalanobis
*************************************
clear all
insheet using ".\stata_data1\data2_io_0.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.9: compustat competition matrix
*************************************
clear all
insheet using ".\stata_data3\data_1.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.9: orbis competition matrix
*************************************
clear all
insheet using ".\stata_data3\data_2.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

*************************************
*Table J.9: hoberg_phillips competition matrix
*************************************
clear all
insheet using ".\stata_data3\data_3.csv"

qui tab tid, gen(year)
qui tab pid, gen(firm)

qui tsset pid tid

newey2 y (z1 z2 = q1 q2) z3 year* firm*, force lag(1)
ivreg2 y (z1 z2 = q1 q2) z3 year* firm*, cluster(pid) partial(year* firm*)

log close
